package conqueror.com.acc2;

import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import java.lang.reflect.InvocationTargetException;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView mTvTime;
    private ImageButton mIbEnsure;
    private ImageButton mIbOff;
    private Timer timer;

    private static final String TAG = MainActivity.class.getName();


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                int arg1 = msg.arg1;
                Log.e("MEA", "--------" + arg1);
                mTvTime.setText("" + arg1);
                if (arg1 == 0) {
                    lockScreen();
                }
            }
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initListener();
        initData();
    }

    private void initView() {
        mTvTime = (TextView) findViewById(R.id.tv_time);
        mIbEnsure = (ImageButton) findViewById(R.id.ib_ensure);
        mIbOff = (ImageButton) findViewById(R.id.ib_off);
    }

    private void initListener() {
        mIbEnsure.setOnClickListener(this);
        mIbOff.setOnClickListener(this);
            }

    /**
     * 计时器
     */
    private void initData() {
        timer = new Timer();
        timer.schedule(new TimerTask() {
            int i = 31;

            @Override
            public void run() {
                --i;
                if (i < 1) {
                    timer.cancel();
                }
                Message msg = new Message();
                msg.what = 1;
                msg.arg1 = i;
                handler.sendMessage(msg);
            }
        }, 1000, 1000);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ib_ensure:
                lockScreen();
                break;
            case R.id.ib_off:
                try {
                    shutDown();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                    Log.e(TAG, "----ClassNotFoundException-------" + e.toString(), e);
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                    Log.e(TAG, "----NoSuchMethodException-------" + e.toString(), e);
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                    Log.e(TAG, "----InvocationTargetException-------" + e.toString(), e);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                    Log.e(TAG, "----IllegalAccessException-------" + e.toString(), e);
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }

    /**
     * 锁屏
     */
    private void lockScreen() {

        // 设备安全管理服务    2.2之前的版本是没有对外暴露的 只能通过反射技术获取
        DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        // 申请权限
        ComponentName componentName = new ComponentName(this, MyAdmin.class);
        // 判断该组件是否有系统管理员的权限
        boolean isAdminActive = devicePolicyManager.isAdminActive(componentName);
        if (isAdminActive) {
            devicePolicyManager.lockNow(); // 锁屏
//          devicePolicyManager.resetPassword("123", 0); // 设置锁屏密码
//          devicePolicyManager.wipeData(0);  //恢复出厂设置  (建议大家不要在真机上测试) 模拟器不支持该操作

        } else {
            Intent intent = new Intent();
            // 指定动作名称
            intent.setAction(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            // 指定给哪个组件授权
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
            startActivity(intent);
        }
        finish();
    }


    /**
     * 关机
     */
    private void shutDown() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {

        Log.v(TAG, "broadcast->shutdown");
        Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
        intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
        //其中false换成true,会弹出是否关机的确认窗口
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
}
